package com.sqly.doc.menu.yyye;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.sqly.doc.R;
import com.sqly.doc.adapter.CommonAdapter;
import com.sqly.doc.adapter.ViewHolder;
import com.sqly.doc.base.MyBaseActivity;
import com.sqly.doc.pojo.RetMsg;
import com.sqly.doc.pojo.XseJtfs;
import com.sqly.doc.pojo.XseSanLiuSuiEtjkjc;
import com.sqly.doc.pojo.XseShiErYueEtjkjc;
import com.sqly.doc.pojo.XseYiBaYueEtjkjc;
import com.sqly.doc.pojo.YcfCh42jkjc;
import com.sqly.doc.util.DateConvert;
import com.sqly.doc.util.HttpUrl;
import com.sqly.doc.util.MyToastUtil;
import com.sqly.doc.util.NetWorkUtils;
import com.sqly.doc.util.SharedPreferencesUtils;
import com.sqly.doc.util.UserClient;
import com.sqly.doc.view.StringUtils;
import com.sqly.doc.view.XListView;
import com.zyao89.view.zloading.ZLoadingDialog;
import com.zyao89.view.zloading.Z_TYPE;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * 育婴育儿列表
 * Created by Administrator on 2017-12-13.
 */

public class yyye_list extends MyBaseActivity {
    XListView listView;
    //标题
    TextView title, uname;
    LinearLayout back;
    ZLoadingDialog dialog;//加载动画
    Context con = yyye_list.this;
    private int pageNum1 = 1;// 无关键字页数
    List<XseJtfs> xselist;//新生儿
    List<XseYiBaYueEtjkjc> yibalist;//1-8月
    List<XseShiErYueEtjkjc> shierlist;//12-30
    List<XseSanLiuSuiEtjkjc> sanliuslist;//3-6
    String type = "";//类型
    String url = "";//请求的路径
    private EditText key;
    private ImageView cx;

    @Override
    protected void initUI() {
        setContentView(R.layout.yyye_list);
        cx=(ImageView)findViewById(R.id.cx);
        cx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pageNum1 = 1;
                getJumin();
            }
        });
        title = (TextView) findViewById(R.id.title);
        uname = (TextView) findViewById(R.id.uname);
        type = getIntent().getStringExtra("type");
        if (type.equals("新生儿家庭访视记录"))
            url = HttpUrl.GetXseList;
        if (type.equals("满月龄儿童健康检查记录") || type.equals("3月龄儿童健康检查记录") || type.equals("6月龄儿童健康检查记录") || type.equals("8月龄儿童健康检查记录"))
            url = HttpUrl.GetYESfjlList;
        if (type.equals("12月龄儿童健康检查记录") || type.equals("18月龄儿童健康检查记录") || type.equals("24月龄儿童健康检查记录") || type.equals("30月龄儿童健康检查记录"))
            url = HttpUrl.GetYE12SfjlList;
        if (type.equals("3岁儿童健康检查记录") || type.equals("4岁儿童健康检查记录") || type.equals("5岁儿童健康检查记录") || type.equals("6岁儿童健康检查记录"))
            url = HttpUrl.GetYE36SfjlList;
        title.setText(type);
        key = (EditText) findViewById(R.id.key);
        uname.setText("新增");
        uname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type.equals("新生儿家庭访视记录"))
                    startActivity(new Intent(con, yyye_xser.class).putExtra("type", "add"));
                if (type.equals("满月龄儿童健康检查记录"))
                    startActivity(new Intent(con, yyye_manyue.class).putExtra("type", "add"));
                if (type.equals("3月龄儿童健康检查记录"))
                    startActivity(new Intent(con, yyye_sanyue.class).putExtra("type", "add"));
                if (type.equals("6月龄儿童健康检查记录"))
                    startActivity(new Intent(con, yyye_liuyue.class).putExtra("type", "add"));
                if (type.equals("8月龄儿童健康检查记录"))
                    startActivity(new Intent(con, yyye_bayue.class).putExtra("type", "add"));
                if (type.equals("12月龄儿童健康检查记录"))
                    startActivity(new Intent(con, yyye_12.class).putExtra("type", "add"));
                if (type.equals("18月龄儿童健康检查记录"))
                    startActivity(new Intent(con, yyye_18.class).putExtra("type", "add"));
                if (type.equals("24月龄儿童健康检查记录"))
                    startActivity(new Intent(con, yyye_24.class).putExtra("type", "add"));
                if (type.equals("30月龄儿童健康检查记录"))
                    startActivity(new Intent(con, yyye_30.class).putExtra("type", "add"));
                if (type.equals("3岁儿童健康检查记录"))
                    startActivity(new Intent(con, yyye_3.class).putExtra("type", "add"));
                if (type.equals("4岁儿童健康检查记录"))
                    startActivity(new Intent(con, yyye_456.class).putExtra("sui", "4").putExtra("type", "add"));
                if (type.equals("5岁儿童健康检查记录"))
                    startActivity(new Intent(con, yyye_456.class).putExtra("sui", "5").putExtra("type", "add"));
                if (type.equals("6岁儿童健康检查记录"))
                    startActivity(new Intent(con, yyye_456.class).putExtra("sui", "6").putExtra("type", "add"));

            }
        });
        back = (LinearLayout) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        listView = (XListView) findViewById(R.id.listView);
        listView.setPullLoadEnable(true);
        dialog = new ZLoadingDialog(con);
        dialog.setLoadingBuilder(Z_TYPE.DOUBLE_CIRCLE)//设置类型
                .setLoadingColor(Color.parseColor("#1C86EE"))//颜色
                .setHintText("Loading...")
                .setHintTextSize(16) // 设置字体大小 dp
                .setHintTextColor(Color.parseColor("#1C86EE"));  // 设置字体颜色
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Bundle b = new Bundle();

                if (type.equals("新生儿家庭访视记录")) {
                    b.putSerializable("b", xselist.get(i - 1));
                    startActivity(new Intent(con, yyye_xser.class).putExtras(b).putExtra("type", "edit"));
                }

                if (type.equals("满月龄儿童健康检查记录")) {
                    b.putSerializable("b", yibalist.get(i - 1));
                    startActivity(new Intent(con, yyye_manyue.class).putExtras(b).putExtra("type", "edit"));
                }

                if (type.equals("3月龄儿童健康检查记录")) {
                    b.putSerializable("b", yibalist.get(i - 1));
                    startActivity(new Intent(con, yyye_sanyue.class).putExtras(b).putExtra("type", "edit"));
                }

                if (type.equals("6月龄儿童健康检查记录")) {
                    b.putSerializable("b", yibalist.get(i - 1));
                    startActivity(new Intent(con, yyye_liuyue.class).putExtras(b).putExtra("type", "edit"));
                }

                if (type.equals("8月龄儿童健康检查记录")) {
                    b.putSerializable("b", yibalist.get(i - 1));
                    startActivity(new Intent(con, yyye_bayue.class).putExtras(b).putExtra("type", "edit"));
                }

                if (type.equals("12月龄儿童健康检查记录")) {
                    b.putSerializable("b", shierlist.get(i - 1));
                    startActivity(new Intent(con, yyye_12.class).putExtras(b).putExtra("type", "edit"));
                }

                if (type.equals("18月龄儿童健康检查记录")) {
                    b.putSerializable("b", shierlist.get(i - 1));
                    startActivity(new Intent(con, yyye_18.class).putExtras(b).putExtra("type", "edit"));
                }
                if (type.equals("24月龄儿童健康检查记录")) {
                    b.putSerializable("b", shierlist.get(i - 1));
                    startActivity(new Intent(con, yyye_24.class).putExtras(b).putExtra("type", "edit"));
                }
                if (type.equals("30月龄儿童健康检查记录")) {
                    b.putSerializable("b", shierlist.get(i - 1));
                    startActivity(new Intent(con, yyye_30.class).putExtras(b).putExtra("type", "edit"));
                }
                if (type.equals("3岁儿童健康检查记录")) {
                    b.putSerializable("b", sanliuslist.get(i - 1));
                    startActivity(new Intent(con, yyye_3.class).putExtras(b).putExtra("type", "edit"));
                }
                if (type.equals("4岁儿童健康检查记录")) {
                    b.putSerializable("b", sanliuslist.get(i - 1));
                    startActivity(new Intent(con, yyye_456.class).putExtras(b).putExtra("sui", "4").putExtra("type", "edit"));
                }
                if (type.equals("5岁儿童健康检查记录")) {
                    b.putSerializable("b", sanliuslist.get(i - 1));
                    startActivity(new Intent(con, yyye_456.class).putExtras(b).putExtra("sui", "5").putExtra("type", "edit"));
                }
                if (type.equals("6岁儿童健康检查记录")) {
                    b.putSerializable("b", sanliuslist.get(i - 1));
                    startActivity(new Intent(con, yyye_456.class).putExtras(b).putExtra("sui", "6").putExtra("type", "edit"));
                }
            }
        });


    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (NetWorkUtils.isNetworkConnected(con)) {//联网状态
            getJumin();
        }
    }

    public void getJumin() {
        RequestParams ps = new RequestParams();
        ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
        ps.add("type", "2");
        ps.add("page", pageNum1 + "");
        ps.add("ylStr",type);
        if (!StringUtils.isEmpty(key.getText().toString()))
            ps.add("XINGMING", key.getText().toString());
      /*  ps.add("XINGMING",SharedPreferencesUtils.getParam(con,"uname","").toString());*/
        UserClient.get(url, ps, new AsyncHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {

                MyToastUtil.ShowToast(con, "请求失败");
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                String content = new String(response);
                dialog.dismiss();
                RetMsg ret = JSON.parseObject(content, RetMsg.class);
                if (ret.getCode().equals("200")) {
                    if (type.equals("新生儿家庭访视记录")) {
                        List<XseJtfs> ll = JSON.parseArray(ret.getList(), XseJtfs.class);
                        if (pageNum1 == 1) {
                            xselist = new ArrayList<XseJtfs>();
                        }
                        for (int i = 0; i < ll.size(); i++) {

                            xselist.add(ll.get(i));
                        }
                        listView.stopRefresh();
                        listView.stopLoadMore();
                        listView.setRefreshTime("刚刚");
                        listView.setXListViewListener(new XListView.IXListViewListener() {
                            @Override
                            public void onRefresh() {
                                pageNum1 = 1;
                                getJumin();
                            }

                            @Override
                            public void onLoadMore() {
                                pageNum1 = pageNum1 + 1;
                                getJumin();
                            }
                        });
                        listView.setAdapter(new CommonAdapter<XseJtfs>(con, xselist, R.layout.ycf_chfsjl_item) {
                            @Override
                            public void convert(ViewHolder helper, XseJtfs item) {
                                helper.setText(R.id.xm, item.getXINGMING());
                                helper.setText(R.id.jkdah, "健康档案号:" + item.getJKDAH());
                                helper.setText(R.id.sfrq, DateConvert.dateToString(DateConvert.toDate(item.getSFRQ(), DateConvert.YMD), DateConvert.YMD)+"    ");
                            }
                        });
                        listView.setSelection(xselist.size() - ll.size());
                    }
                    if (type.equals("满月龄儿童健康检查记录") || type.equals("3月龄儿童健康检查记录") || type.equals("6月龄儿童健康检查记录") || type.equals("8月龄儿童健康检查记录")) {
                        List<XseYiBaYueEtjkjc> ll = JSON.parseArray(ret.getList(), XseYiBaYueEtjkjc.class);
                        if (pageNum1 == 1) {
                            yibalist = new ArrayList<XseYiBaYueEtjkjc>();
                        }
                        for (int i = 0; i < ll.size(); i++) {

                            yibalist.add(ll.get(i));
                        }
                        listView.stopRefresh();
                        listView.stopLoadMore();
                        listView.setRefreshTime("刚刚");
                        listView.setXListViewListener(new XListView.IXListViewListener() {
                            @Override
                            public void onRefresh() {
                                pageNum1 = 1;
                                getJumin();
                            }

                            @Override
                            public void onLoadMore() {
                                pageNum1 = pageNum1 + 1;
                                getJumin();
                            }
                        });
                        listView.setAdapter(new CommonAdapter<XseYiBaYueEtjkjc>(con, yibalist, R.layout.ycf_chfsjl_item) {
                            @Override
                            public void convert(ViewHolder helper, XseYiBaYueEtjkjc item) {
                                helper.setText(R.id.xm, item.getXINGMING());
                                helper.setText(R.id.jkdah, "健康档案号:" + item.getJKDAH());
                                helper.setText(R.id.sfrq, DateConvert.dateToString(DateConvert.toDate(item.getSFRQ(), DateConvert.YMD), DateConvert.YMD)+"    ");
                            }
                        });
                        listView.setSelection(yibalist.size() - ll.size());
                    }
                    if (type.equals("12月龄儿童健康检查记录")||type.equals("18月龄儿童健康检查记录")||type.equals("24月龄儿童健康检查记录")||type.equals("30月龄儿童健康检查记录")) {
                        List<XseShiErYueEtjkjc> ll = JSON.parseArray(
                                ret.getList(), XseShiErYueEtjkjc.class);
                        if (pageNum1 == 1) {
                            shierlist = new ArrayList<XseShiErYueEtjkjc>();
                        }
                        for (int i = 0; i < ll.size(); i++) {

                            shierlist.add(ll.get(i));
                        }
                        listView.stopRefresh();
                        listView.stopLoadMore();
                        listView.setRefreshTime("刚刚");
                        listView.setXListViewListener(new XListView.IXListViewListener() {
                            @Override
                            public void onRefresh() {
                                pageNum1 = 1;
                                getJumin();
                            }

                            @Override
                            public void onLoadMore() {
                                pageNum1 = pageNum1 + 1;
                                getJumin();
                            }
                        });
                        listView.setAdapter(new CommonAdapter<XseShiErYueEtjkjc>(con, shierlist, R.layout.ycf_chfsjl_item) {
                            @Override
                            public void convert(ViewHolder helper, XseShiErYueEtjkjc item) {
                                helper.setText(R.id.xm, item.getXINGMING());
                                helper.setText(R.id.jkdah, "健康档案号:" + item.getJKDAH());
                                helper.setText(R.id.sfrq, DateConvert.dateToString(DateConvert.toDate(item.getSFRQ(), DateConvert.YMD), DateConvert.YMD)+"    ");
                            }
                        });
                        listView.setSelection(shierlist.size() - ll.size());
                    }
                    if (type.equals("3岁儿童健康检查记录")||type.equals("4岁儿童健康检查记录")||type.equals("5岁儿童健康检查记录")||type.equals("6岁儿童健康检查记录")) {
                        List<XseSanLiuSuiEtjkjc> ll = JSON.parseArray(
                                ret.getList(), XseSanLiuSuiEtjkjc.class);
                        if (pageNum1 == 1) {
                            sanliuslist = new ArrayList<XseSanLiuSuiEtjkjc>();
                        }
                        for (int i = 0; i < ll.size(); i++) {

                            sanliuslist.add(ll.get(i));
                        }
                        listView.stopRefresh();
                        listView.stopLoadMore();
                        listView.setRefreshTime("刚刚");
                        listView.setXListViewListener(new XListView.IXListViewListener() {
                            @Override
                            public void onRefresh() {
                                pageNum1 = 1;
                                getJumin();
                            }

                            @Override
                            public void onLoadMore() {
                                pageNum1 = pageNum1 + 1;
                                getJumin();
                            }
                        });
                        listView.setAdapter(new CommonAdapter<XseSanLiuSuiEtjkjc>(con, sanliuslist, R.layout.ycf_chfsjl_item) {
                            @Override
                            public void convert(ViewHolder helper, XseSanLiuSuiEtjkjc item) {
                                helper.setText(R.id.xm, item.getXINGMING());
                                helper.setText(R.id.jkdah, "健康档案号:" + item.getJKDAH());
                                helper.setText(R.id.sfrq, DateConvert.dateToString(DateConvert.toDate(item.getSFRQ(), DateConvert.YMD), DateConvert.YMD)+"    ");
                            }
                        });
                        listView.setSelection(sanliuslist.size() - ll.size());
                    }
                } else {
                    if (pageNum1 == 1)
                        listView.setAdapter(new CommonAdapter<YcfCh42jkjc>(con, new ArrayList<YcfCh42jkjc>(), R.layout.ycf_chfsjl_item) {
                            @Override
                            public void convert(ViewHolder helper, YcfCh42jkjc item) {
                                helper.setText(R.id.xm, item.getXINGMING());
                                helper.setText(R.id.jkdah, "健康档案号:" + item.getJKDAH());
                                helper.setText(R.id.sfrq, DateConvert.dateToString(DateConvert.toDate(item.getSFRQ(), DateConvert.YMD), DateConvert.YMD)+"    ");
                            }
                        });
                }
            }
        });
    }
}
